package com.NeuglsWorkStudio.UI.FormElement.Component
{
	import com.NeuglsWorkStudio.Colors.ColorUtility;
	import com.NeuglsWorkStudio.UI.FormElement.Config.TConfig;
	import com.NeuglsWorkStudio.UI.FormElement.Config.TListBoxConfig;
	import com.NeuglsWorkStudio.UI.FormElement.Config.TListItemConfig;
	import com.NeuglsWorkStudio.UI.ScrollBar;
	import com.greensock.TweenLite;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	public class TListBox extends TComponent
	{
		private var _FBack:Sprite;
		private var _FContainer:Sprite;
		private var _FListItems:Vector.<TListItem>;
		private var _FScrollBar:ScrollBar;
		private var _FItemIndex:int=-1;
		private var _FScrollLine:Sprite;
		private var _FScrollSlider:Sprite;
		
		public static var ListItemHeight:Number=85;
		public static var MarginY:Number=0;
		public static var ScrollBarWidth:Number=8;
		public static var ScrollBarHeight:Number=15;
		public static var ScrollLineWidth:Number=10;
		
		public static var TransitionTime:Number=0.5;
		
		public function TListBox(AConfig:TConfig)
		{
			super(AConfig);
		}
		
		public function ListConfig():TListBoxConfig{
			return _FConfig as TListBoxConfig;
		}
		
		private function GetFunction(F:Function,AToCall:Function=null):Function{
			return function(ID:int,ExtraData:Object):void{
				if(AToCall==null){
					OnItemClick(ID,F,ExtraData);
				}else{
					F(ID,ExtraData);
				}
			}
		}
		
		private function OnItemClick(ID:int,F:Function,ExtraData:Object):void{
			_FItemIndex=ID;
			if(F!=null){
				F(ExtraData);
			}
		}
		
		private function OnListItemDeleteClick(ID:int,ExtraData:Object):void{
			RemoveListItem(ID);
		}
		
		protected override function Init():void{
			super.Init();
			
			_FBack=new Sprite();
			_FContainer=new Sprite();
			this.addChild(_FBack);
			this.addChild(_FContainer);
			_FListItems=new Vector.<TListItem>();
			
			var W:Number=ListConfig().HaveBorder?ListConfig().Width-ListConfig().BorderSize:ListConfig().Width;
			for(var i:int=0,L:int=ListConfig().Items.length;i<L;i++){
				var ListItemConfig:TListItemConfig=new TListItemConfig();
				BuildListItemConfig(ListItemConfig);
				ListItemConfig.Caption=ListConfig().Items[i].Caption;
				ListItemConfig.DisplayIcon=ListConfig().Items[i].DisplayIcon;
				ListItemConfig.ExtraData=ListConfig().Items[i].ExtraData;
				ListItemConfig.Icon=ListConfig().Items[i].Icon;
				ListItemConfig.IconH=ListConfig().Items[i].IconH;
				ListItemConfig.IconW=ListConfig().Items[i].IconW;
				ListItemConfig.CenterIcon=true;
				ListItemConfig.IconDisplayHeight=ListItemConfig.Height;
				ListItemConfig.IconDisplayWidth=99;
				ListItemConfig.IconURL=ListConfig().Items[i].IconURL;
				ListItemConfig.Name='listItem'+i;
				ListItemConfig.Width=W;
				ListItemConfig.OptionalButtons={Delete:true,OnDelete:GetFunction(OnListItemDeleteClick,OnListItemDeleteClick)};
				
				var ExtraData:Object=ListConfig().Items[i].ExtraData;
				var F:Function=ListConfig().OnClickItem;
				
				ListItemConfig.OnClick=GetFunction(F);
				
				var ListItem:TListItem=new TListItem(ListItemConfig);
				ListItem.ID=i;
				_FContainer.addChild(ListItem);
				_FListItems.push(ListItem);
			}
			Paint();
			
			this.addEventListener(Event.ADDED_TO_STAGE,Setup,false,0,true);
			this.addEventListener(Event.REMOVED_FROM_STAGE,Destroy,false,0,true);
		}
		
		private function Setup(E:Event):void{
			_FScrollLine=GetScrollObject(cScrollLine);
			_FScrollSlider=GetScrollObject(cScrollBar);
			
			var Border:Number=ListConfig().HaveBorder?ListConfig().BorderSize:0;
			var SliderX:Number=ListConfig().Width-_FScrollLine.width-Border;
			var MaskWidth:Number=SliderX-Border;	
			_FScrollLine.x=SliderX;
			_FScrollSlider.x=_FScrollLine.x+(_FScrollLine.width-_FScrollSlider.width)/2;
			
			_FScrollBar=new ScrollBar(_FContainer,
				new Rectangle(0,Border,MaskWidth,ListConfig().Height-2*Border),
				_FScrollSlider,
				_FScrollLine,5,true		
			);		
			_FScrollBar.mouseWheel=true;			 
			_FScrollBar.useKeyBoard=true;			
		}
		
		private function Destroy(E:Event):void{
			_FScrollBar=null;
			_FScrollSlider=null;
			_FScrollLine=null;
			_FListItems=null;
			_FBack=null;
			_FContainer=null;
		}
		
		private const cScrollBar:int=1;
		private const cScrollLine:int=2;
		
		/**获取滚动对象
		 * @param Kind 类型,可取值cScrollBar,cScrollLine*/
		private function GetScrollObject(Kind:int):Sprite{
			var r:Sprite=new Sprite();
			var W:Number;
			var H:Number;
			var _FColor:int;
			var Radius:Number=10;
			switch(Kind){
				case cScrollBar:
					W=ScrollBarWidth;
					H=ScrollBarHeight;
					_FColor=ListConfig().BaseColor;
					break;
				case cScrollLine:
					W=ScrollLineWidth;
					H=ListConfig().Height;
					_FColor=ColorUtility.GetBackColorFromBaseColor(ListConfig().BaseColor);
					break;
			}
			
			with(r.graphics){
				clear();
				beginFill(_FColor,1);
				drawRoundRect(0,0,W,H,Radius,Radius);
				endFill();
			}
			
			this.addChild(r);
			return r;
		}
		
		public function BuildListItemConfig(ListItemConfig:TListItemConfig):void{
			ListItemConfig.Enabled=true;
			ListItemConfig.HoverBackAlpha=0.4;
			ListItemConfig.HoverBackColor=ColorUtility.GetBackColorFromBaseColor(ListConfig().BaseColor);
			ListItemConfig.HoverBorderColor=ListConfig().BaseColor;
			ListItemConfig.HoverBorderSize=1;
			ListItemConfig.HoverFontColor=ColorUtility.GetColorFromBaseColor(ListConfig().BaseColor);
			
			ListItemConfig.NormalBackAlpha=0.0;
			ListItemConfig.NormalBackColor=ColorUtility.GetBackColorFromBaseColor(ListConfig().BaseColor);
			ListItemConfig.NormalBorderColor=ListConfig().BaseColor;
			ListItemConfig.NormalBorderSize=1;
			ListItemConfig.NormalFontColor=ColorUtility.GetColorFromBaseColor(ListConfig().BaseColor);
			
			ListItemConfig.DownBackAlpha=0.1;
			ListItemConfig.DownBackColor=ColorUtility.GetBackColorFromBaseColor(ListConfig().BaseColor);
			ListItemConfig.DownBorderColor=ListConfig().BaseColor;
			ListItemConfig.DownBorderSize=1;
			ListItemConfig.DownFontColor=ColorUtility.GetColorFromBaseColor(ListConfig().BaseColor);
			
			ListItemConfig.Height=ListItemHeight;
			
		}
		
		protected function Paint():void{
			var LastY:Number=2;
			var LastX:Number=this.ListConfig().HaveBorder?ListConfig().BorderSize:0;
			
			var BW:Number=this.ListConfig().HaveBorder?this.ListConfig().Width:this.ListConfig().Width-this.ListConfig().BorderSize*2;
			var BH:Number=this.ListConfig().HaveBorder?this.ListConfig().Height:this.ListConfig().Height-this.ListConfig().BorderSize*2;;
			var R:Number=5;
			var BorderColor:int=ColorUtility.GetBorderColorFromBaseColor(ListConfig().BaseColor);
			var BackColor:int=ColorUtility.GetBackColorFromBaseColor(ListConfig().BaseColor);
			
			//draw background
			with(_FBack.graphics){
				clear();
				if(ListConfig().HaveBorder)
					lineStyle(ListConfig().BorderSize,BorderColor);
				beginFill(BackColor,ListConfig().BackAlpha);
				drawRoundRect(0,0,BW,BH,R,R);	
				endFill();
			}
			_FBack.x=0;
			_FBack.y=0;
			
			for(var I:int=0;I<_FListItems.length;I++){
				_FListItems[I].x=LastX;
				_FListItems[I].y=LastY;
				_FListItems[I].Width=BW;
				_FListItems[I].ID=I;
				LastY+=_FListItems[I].height+MarginY;
			}
			
			if(_FScrollBar)
				_FScrollBar.refresh();
		}
		
		
		/**添加一个ListItem项*/
		public function AddListItem(ItemConfig:TListItemConfig):uint{
			if(!ItemConfig) return 0;
			
			BuildListItemConfig(ItemConfig);
			var ExtraData:Object=ItemConfig.ExtraData;
			var F:Function=ListConfig().OnClickItem;
			
			ItemConfig.OnClick=GetFunction(F);
			ItemConfig.OptionalButtons={Delete:true,OnDelete:GetFunction(OnListItemDeleteClick,OnListItemDeleteClick)};
			ItemConfig.Width=ListConfig().HaveBorder?ListConfig().Width-ListConfig().BorderSize:ListConfig().Width;
			ItemConfig.CenterIcon=true;
			ItemConfig.IconDisplayHeight=ItemConfig.Height;
			ItemConfig.IconDisplayWidth=99;
			ItemConfig.BaseColor=this.ListConfig().BaseColor;
			
			var Item:TListItem=new TListItem(ItemConfig);
			_FContainer.addChild(Item);
			var r:uint=_FListItems.push(Item);
			Item.alpha=0;
			Paint();
			TweenLite.to(Item,TransitionTime,{alpha:1});
			
			return r;
		}
		
		private var _FIsDeleting:Boolean=false;
		
		public function RemoveListItem(Index:int):void{
			if(Index<0||Index>_FListItems.length-1) return ;
			if(_FIsDeleting) return;
			_FIsDeleting=true;
			var DO:DisplayObject=_FListItems[Index];
			TweenLite.to(DO,TransitionTime,{alpha:0,onComplete:DoRemove,onCompleteParams:[Index]});
		}
		
		private function DoRemove(Index:int):void{
			var DO:DisplayObject=_FContainer.removeChild(_FListItems[Index]);
			if(ListConfig().OnItemDelete!=null){
				ListConfig().OnItemDelete((DO as TListItem).ExtraData);
			}
			DO=null;
			_FListItems.splice(Index,1);
			Paint();
			_FIsDeleting=false;
		}
		
		public function get ItemIndex():int{
			return this._FItemIndex;
		}
		
		public function GetListItemWithItemName(name:String):TListItem{
			return _FContainer.getChildByName(name) as TListItem;
		}
		
		public function ModifyListItemCaptionWithName(name:String,Caption:String):void{
			GetListItemWithItemName(name).Caption=Caption;
		}
		
		public function Resize(W:Number,H:Number):void{
			this.ListConfig().Width=W;
			this.ListConfig().Height=H;
			Paint();
		}
	}
}